package dk.itu.samd.group2.tagapastry;

import rice.p2p.commonapi.IdFactory;

public class TagFactory {

	private static IdFactory idFactory;
	
	public static void setIdFactory(IdFactory idFactory) {
		TagFactory.idFactory = idFactory;
	}
	
	public static Tag generateTag(String keyword, String type) {
		return new Tag(idFactory, keyword, type);
	}
	
	// Generated when an index generates messages that address its children
	public static Tag duplicateTagWithExtension(Tag tag, char extension) {
		//return new Tag(idFactory, tag.getKeyword(), tag.getType(), tag.getExtension() + extension);
		Tag ntag = new Tag(idFactory, tag.getKeyword(), tag.getType(), tag.getExtension() + extension);
		return getComplex(tag,ntag);
	}
	
	// Called from IndexExpansionMsg when an IndexExpansionConfirmation is generated.
	// A character must be chopped off the extension so we can identify the parent of this index
	public static Tag duplicateTagMinusExtension(Tag tag) {
		String ext = tag.getExtension();
		//return new Tag(idFactory, tag.getKeyword(), tag.getType(), ext.substring(0, ext.length() - 1));
				
		Tag ntag = new Tag(idFactory, tag.getKeyword(), tag.getType(), ext.substring(0, ext.length() - 1));	
		return getComplex(tag,ntag);
	}
	
	private static Tag getComplex(Tag tag, Tag ntag) {
		if(tag.isComplex()) {
			ntag.makeComplex();
			ntag.setAlternateName(tag.getAlternateName());
		}
		
		return ntag;
	}
	
	public static Tag duplicateTagAlternateName(Tag tag) {
		Tag ntag = new Tag(idFactory, tag.getAlternateName(),tag.getType());
		return getComplex(tag,ntag);
	}
	
}
